<%--
  Created by IntelliJ IDEA.
  User: houxianghua
  Date: 2015/7/10
  Time: 8:59
  To change this template use File | Settings | File Templates.
--%>

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/WEB-INF/jsp/component/common.jsp" %>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">
    <title></title>
    <%@include file="/WEB-INF/jsp/component/commonHead.jsp" %>
    <%@include file="/WEB-INF/jsp/component/commonBottom.jsp" %>
    <style>
        .text-center{
            text-align:center;
        }
    </style>
</head>
<%--<body onresize="reSizeParentIframe()" onload="reSizeParentIframe();">--%>
<body>
    <div class="row">
        <div class="col-lg-12">
            <h1 class="page-header">医院管理</h1>
        </div>
        <!-- /.col-lg-12 -->
    </div>
    <!-- /.row -->
    <div class="row">
        <div class="col-lg-12">
            <div class="panel panel-default">
                <div class="panel-heading">
                    所属地区:
                    <select id="largeArea" style="width: 100px;height: 24px;">
                        <option value="">大区</option>
                        <c:forEach items="${largeAreaList}" var="largeArea">
                            <option value="${largeArea.largeAreaCode}">${largeArea.largeAreaName}</option>
                        </c:forEach>
                    </select>
                    <select id="provinceArea" style="width: 100px;height: 24px;">
                        <option value="">省</option>
                    </select>
                    <select id="cityArea" style="width: 100px;height: 24px;">
                        <option value="">市</option>
                    </select>
                    医院等级:
                    <select id="hospLvlList" name="hospLvlList" >
                        <option value="" >医院等级</option>
                        <c:forEach items="${hospLvlList}" var="Dict">
                            <option value="${Dict.code}">${Dict.name}</option>
                        </c:forEach>
                    </select>
                    医院名称:<input type="text" id="hospName" name="hospName"/>
                    <button id="query" class="mk_butt" >查询</button>
                    <%--<button id="query" type="button" class="btn btn-outline btn-success">查询</button>--%>
                </div>
                <!-- /.panel-heading -->
                <div class="panel-body">
                    <div class="dataTable_wrapper">
                        <table id="example" class="table table-striped table-bordered table-hover" width="100%">
                            <thead>
                            <tr align="left">
                                <th><input id="all_id" type="checkbox"></th>
                                <th>医院名称</th>
                                <th>医院等级</th>
                                <th>医院地址</th>
                                <th>医院联系电话</th>
                                <%--<th>操作</th>--%>
                            </tr>
                            </thead>
                            <tbody>
                            </tbody>
                        </table>
                    </div>
                </div>
                <!-- /.panel-body -->
            </div>
            <!-- /.panel -->
        </div>
        <!-- /.col-lg-12 -->
    </div>

    <script>

        var showLagreArea=false;

        $(function () {
            $("#largeArea").change(function () {
                if (this.value == null || this.value == "") {
                    resetArea(0);
                } else {
                    getLowerAreaList("provinceArea", this.value, 0);
                    resetArea(1);
                }
            });
            $("#provinceArea").change(function () {
                if (this.value == null || this.value == "") {
                    resetArea(1);
                } else {
                    getLowerAreaList("cityArea", this.value, 1);
                    resetArea(2);
                }
            });
        });
        /**
         * 后台获取下级列表
         * */
        function getLowerAreaList(id,code,level){
            $.ajax({
                url:'${ctx}/area/getLowerAreaList',
                type:'post',
                data:'code='+code+"&level="+level,
                async : false, //默认为true 异步
                error:function(){
                    alert('error');
                },
                success:function(data){
                    var objs = document.getElementById(id);
                    objs.options.length = 1;
                    var area = data;
                    for(var i=0;i<area.length;i++){
                        var childOption = document.createElement("option");
                        childOption.value = area[i].code;
                        childOption.innerHTML = area[i].name;
                        objs.appendChild(childOption);
                    }
                }
            });
        }

        var table;
        function initTable() {
            table = $("#example").DataTable({
                "dom": "<'row'<'col-xs-2'l><'#mytool.col-xs-10'><'col-xs-6'f>r>" +
                "t" +
                "<'row'<'col-xs-6'i><'col-xs-6'p>>",
                "bFilter": false,//去掉搜索框
                "bAutoWidth": true, //自适应宽度
                "sPaginationType" : "full_numbers",
//                "bDestroy" : true,
//              "asStripeClasses": [ 'strip1', 'strip2', 'strip3' ],//设置斑马条（奇偶行）的css class
                "ordering": false,//是否允许Datatables开启排序
                "bProcessing" : true,
                "bServerSide" : true,
                "sAjaxSource" : "${URL_ADM_HOSP_SEARCH}",
                "sAjaxDataProp": "dataList",//查询后，返回的集合
                "fnServerData": function ( sSource, aoData, fnCallback ) {  //查询条件
                    aoData.push(
                            { "name": "hospName", "value": $("#hospName").val() }
                    );
                    aoData.push(
                            { "name": "areaCode", "value":  getAreaCode() }
                    );
                    aoData.push(
                            { "name": "hosLevel", "value":  $("#hospLvlList").val()}
                    );
                    aoData.push(
                            { "name": "showLagreAreaDec", "value": showLagreArea }
                    );
                    $.ajax( {
                        "dataType": 'json',
                        "url": sSource,
                        "type":'post',
                        "data": aoData,
                        "success": fnCallback
                    } );

                },
                "aoColumns" : [ {
                    "targets": -1,
                    "data": null,
                    "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                        $(nTd).html('' +
                                '&nbsp;&nbsp;<input name="id" value="'+oData.id+'" type="checkbox" onClick="isCheckedAll()">' +
                                '');
                    }
                }, {
                    "mDataProp" : "hospName",
                    'sClass': "text-center"
                }, {
                    "mDataProp" : "hosLevel",
                    "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
                        var levelName = "";
                        <c:forEach items="${hospLvlList}" var="level">
                            if('${level.code}'==sData){
                                levelName = "${level.name}";
                            }
                        </c:forEach>
                        $(nTd).html(levelName);
                    }
                }, {
                    "mDataProp" : "hospAddress",
                }, {
                    "mDataProp" : "contactPhone",
                }
//                    ,{
//                        "targets": 5,
//                        "data": null,
//                        "fnCreatedCell": function (nTd, sData, oData, iRow, iCol) {
//                            $(nTd).html('' +
//                                    '<button type="button" id="update_butt" class="btn btn-outline btn-primary" >修改</button>&nbsp;&nbsp;&nbsp;&nbsp;' +
//                                    '<button type="button" id="delete_butt" class="btn btn-outline btn-danger">删除</button>' +
//                                    '');
//                        }
//                    }
                ],
                "oLanguage" : {
                    "sProcessing" : "正在加载中......",
                    "sLengthMenu" : "每页显示 _MENU_ 条记录",
                    "sZeroRecords" : "没有数据！",
                    "sEmptyTable" : "无数据存在！",
                    "sInfo" : "当前显示 _START_ 到 _END_ 条，共 _TOTAL_ 条记录",
                    "sInfoEmpty" : "显示0到0条记录",
                    "sInfoFiltered" : "数据表中共为 _MAX_ 条记录",
                    //"sSearch" : "搜索",
                    "oPaginate" : {
                        "sFirst" : "首页",
                        "sPrevious" : "上一页",
                        "sNext" : "下一页",
                        "sLast" : "末页"
                    }
                },
                initComplete: function () {
                    <shiro:hasPermission name="+hospital+8">
                    $("#mytool").append('<button id="delete_butt" type="button" class="btn btn-outline btn-success" style="float:right;">删除</button>');
                    $("#delete_butt").on("click", toDelete);
                    </shiro:hasPermission>
                    <shiro:hasPermission name="+hospital+4">
                    $("#mytool").append('<button id="update_butt" type="button" class="btn btn-outline btn-success" style="float:right;">修改</button>');
                    $("#update_butt").on("click", toUpdate);
                    </shiro:hasPermission>
                    <shiro:hasPermission name="+hospital+2">
                    $("#mytool").append('<button id="add_butt" type="button" class="btn btn-outline btn-success" style="float:right;">新增</button>');
                    $("#add_butt").on("click", toAdd);
                    </shiro:hasPermission>
                    <shiro:hasPermission name="+hospital">
                    $("#mytool").append('<button id="export_butt" type="button" class="btn btn-outline btn-success" style="float:right;">导出</button>');
                    $("#export_butt").on("click", toExport);
                    </shiro:hasPermission>
                    <shiro:hasPermission name="+hospital+2">
                    $("#mytool").append('<button id="import_butt" type="button" class="btn btn-outline btn-success" style="float:right;">导入</button>');
                    $("#import_butt").on("click", toImport);
                    </shiro:hasPermission>
                }
            });


            //修改
//            $('#example tbody').on( 'click', '#update_butt', function (e) {
//                var rowData = table.row( $(this).parents('tr') ).data();
//                $("#updateIframe").attr("src", '/hosp/toUpdate?id=' +rowData.id);//修改地址
//                $('#updateModal').modal({ show: true, backdrop: 'static' });
//                var width = 800;
//                var height = 920;
//                var mycss={"width":width+"px","height":height+"px","top":(($(window).height()-height)/2>0?($(window).height()-height)/2:0)+"px"};
//                $('#updateModal .modal-dialog').css(mycss).find('.modal-content').css({height: '100%',width: '100%'}).find('.modal-body').css({height: '88%' });
//            });

        }
        /**
         * 重置下级菜单
         * */
        function resetArea(level){
            if(level<1){
                var provinceArea = document.getElementById("provinceArea");
                provinceArea.options.length = 1;
            }
            if(level<2){
                var cityArea = document.getElementById("cityArea");
                cityArea.options.length = 1;
            }
//            if(level<3){
//                var countyArea = document.getElementById("countyArea");
//                countyArea.options.length = 1;
//            }
//            if(level<4){
//                var townArea = document.getElementById("townArea");
//                townArea.options.length = 1;
//            }
//            if(level<5){
//                var villageArea = document.getElementById("villageArea");
//                villageArea.options.length = 1;
//            }
        }
        function getAreaCode(){
            showLagreArea=false;
            if($("#cityArea").val()!=""){
                return $("#cityArea").val();
            }
            if($("#provinceArea").val()!=""){
                return $("#provinceArea").val();
            }
            if($("#largeArea").val()!=""){
                showLagreArea=true;
                return $("#largeArea").val();
            }
            return "";
        }


        //新增
        function toAdd(){
            window.parent.showModelIframe('${URL_ADM_HOSP_TOADD}',"新增医院",800,800);
        }

        //修改
        function toUpdate(){
            var checkedArray = getCheckedArray();
            if(checkedArray.length!=1){
                top.bootbox.dialog({
                    message: "请选中一项数据",
                    buttons: {
                        OK: {
                            label: "确定",
                            className: "btn-primary"
                        }
                    }
                });
                return false;
            }
            window.parent.showModelIframe('${URL_ADM_HOSP_TOUPDATE}?id=' +checkedArray[0],"修改医院",800,800);
        }

        //删除
        function toDelete(){
            var checkedArray = getCheckedArray();
            var ids = "";
            if(checkedArray.length==0){
                top.bootbox.dialog({
                    message: "请选中至少一项数据",
                    buttons: {
                        OK: {
                            label: "确定",
                            className: "btn-primary"
                        }
                    }
                });
                return false;
            }else{
                for(var i=0; i<checkedArray.length;i++){
                    ids += checkedArray[i]+",";
                }
                ids = ids.substring(0,ids.length-1);
            }
            top.bootbox.dialog({
//                message: "医院："+rowData.hospName,
//                title: "确定删除此项？",
                message: "确定删除此项？",
                buttons: {
                    OK: {
                        label: "确定",
                        className: "btn-primary",
                        callback: function () {
                            $.ajax({
                                url:'${URL_ADM_HOSP_DELETES}',//删除地址
                                type:'post',
                                data:{ids:ids},
                                async : false, //默认为true 异步
                                error:function(){
                                    alert('error');
                                },
                                success:function(data){
                                    refreshTable();
                                }
                            });
                        }
                    },
                    Cancel: {
                        label: "取消",
                        className: "btn-default"
                    }
                }
            });
        }

        //多选按钮
        $("#all_id").click(function(){
            if(this.checked){
                $("input[name='id']").each(function(){
                    $(this)[0].checked = true;
                });
            }else{
                $("input[name='id']").each(function(){
                    $(this)[0].checked = false;
                });
            }
        });

        //判断是否已经全选
        function isCheckedAll(){
            var isCheckAll = true;
            $("input[name='id']").each(function(){
                if(!this.checked){
                    isCheckAll = false;
                }
            });
            if(isCheckAll){
                $("#all_id")[0].checked = true;
            }else{
                $("#all_id")[0].checked = false;
            }
        }

        function getCheckedArray(){
            var checkedArray = [];
            $("input[name='id']").each(function(){
                if(this.checked){
                    checkedArray.push(Number(this.value));
                }
            });
            return checkedArray;
        }

//        //新增提交  调用子级页面新增方法
//        $("#submit_add").click(function(){
//            document.getElementById('addIframe').contentWindow.doAdd();
//        });
//
//        //修改提交  调用子级页面修改方法
//        $("#submit_update").click(function(){
//            document.getElementById('updateIframe').contentWindow.doUpdate();
//        });

        //查询
        $("#query").click(function() {
            refreshTable();
        });

        //导入
        function toImport() {
            window.parent.showModelIframe('${URL_ADM_HOSP_TOIMPORT}',"导入Excel",800,400);
        }

        //导出
        function toExport() {
            window.parent.showModelIframe('${URL_ADM_HOSP_TOEXPORT}',"导出Excel",800,400);
            <%--location.href = '${URL_ADM_HOSP_EXPORT}?companyName='+$("#companyName").val()+"&areaCode="+getAreaCode()+"&level="+$("#level").val()+"&showLagreAreaDec="+showLagreArea;--%>
        }

        //刷新table数据
        function refreshTable(){
            table.ajax.reload();
            //重置全选按钮
            $("#all_id")[0].checked = false;
        }

        $(document).ready(function() {
            initTable();
        });
    </script>
</body>
</html>